package com.guan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.ruiji.common.BaseContext;
import com.guan.ruiji.common.R;
import com.guan.ruiji.pojo.Category;
import com.guan.ruiji.service.CategoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author guanzhenjie
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @RequestMapping("getCategoryList")
    public R<Page> getCategoryList(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize){
        Page page = new Page(currentPage, pageSize);
        // 查询
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Category::getIsDeleted, 0);
        lambdaQueryWrapper.orderByAsc(Category::getType);
        lambdaQueryWrapper.orderByAsc(Category::getSort);

        categoryService.page(page, lambdaQueryWrapper);
        return R.success(page);
    }

    @RequestMapping("getCategoryListByType")
    public R<List> getCategoryList(@Param("type") Integer type){
        // 查询
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Category::getIsDeleted, 0);
        lambdaQueryWrapper.eq(Category::getType, type);

        List<Category> list = categoryService.list(lambdaQueryWrapper);

        return R.success(list);
    }

    @RequestMapping("add")
    public R<String> add(HttpServletRequest request, @RequestBody Category category){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        boolean success = categoryService.save(category);

        if (success) {
            return R.success("新增分类成功");
        } else {
            return R.error("新增分类失败");
        }
    }

    @RequestMapping("update")
    public R<String> update(HttpServletRequest request, @RequestBody Category category){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        // 更新
        boolean success = categoryService.updateById(category);

        if (success) {
            return R.success("更新分类成功");
        } else {
            return R.error("更新分类失败");
        }
    }

    @RequestMapping("delete")
    public R<String> delete(HttpServletRequest request, @RequestBody Category category){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        // 更新
        boolean success = categoryService.remove(category.getId());

        if (success) {
            return R.success("删除分类成功");
        } else {
            return R.error("删除分类失败");
        }
    }
}
